﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate.Transform;

using ProjectBase.Core;

using $prjsn$.Entity;

namespace $safeprojectname$
{
    public class AdditionalTransformer<TEntity> : IResultTransformer
        where TEntity : class, IBusinessObject, new()
    {
        // rows iterator
        public object TransformTuple(object[] tuple, string[] aliases)
        {
            var list = new List<string>(aliases);

            var newAliases = new List<string>(aliases);
            for (var index = 0; index < newAliases.Count; index++)
            {
                if (newAliases[index].StartsWith("Additional" + '.'))
                {
                    newAliases[index] = null;
                }
            } 

            var result = Transformers.AliasToBean<TEntity>().TransformTuple(tuple, newAliases.ToArray());

            var additional = list
                .Where(a => a.StartsWith("Additional" + '.'))
                .ToDictionary(a => a, a => a.Split('.')[1])
                .ToList();

            var contact = result as TEntity;

            foreach (var pair in additional)
            {
                var key = pair.Key;
                var name = pair.Value;
                var index = list.IndexOf(key);

                contact.Additional[name] = tuple[index];
            }
            return result;
        }

        // convert to DISTINCT list with populated Fields
        public System.Collections.IList TransformList(System.Collections.IList collection)
        {
            var results = Transformers.AliasToBean<TEntity>().TransformList(collection);
            return results;
        }
    }
}